package com.isesol.ime.validator.message;

import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.enums.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;

import javax.annotation.*;

@Component
public class MessageUpdateValidator extends ValidatorHandler<MessageDTO> implements Validator<MessageDTO> {

    @Resource
    MessageMapper messageMapper;

    @Resource
    ParamMapper paramMapper;

    @Resource
    MessageCreateValidator messageCreateValidator;

    @Override
    public boolean validate(ValidatorContext context, MessageDTO messageDTO) {

        boolean result = true;

        if (StringUtils.isEmpty(messageDTO.getMsgId())) {

            context.addErrorMsg("params error,msgId is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getMsgName())) {

            context.addErrorMsg("params error,msgName is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getMsgNumber())) {

            context.addErrorMsg("params error,msgNumber is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getRuleType())) {

            context.addErrorMsg("params error,ruleType is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getRule())) {

            context.addErrorMsg("params error,rule is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getParams())) {
            context.addErrorMsg("params error,params is null");
            result = false;

        }

        if (StringUtils.isNotEmpty(String.valueOf(messageDTO.getId()))) {

            if (null == messageMapper.getById(messageDTO.getId())) {

                context.addErrorMsg("params error,"+ErrorCode.MESSAGE_NOT_EXISTS.getMessage() + ",id = " + messageDTO.getId());
                result = false;

            }
        }

        if (!validateMsgIdUpdate(messageDTO)) {

            context.addErrorMsg("params error,"+ErrorCode.MESSAGEID_NOT_COMPARE.getMessage() + ",mgsId = " + messageDTO.getMsgId());
            result = false;
        }

        String paramIds = messageDTO.getParams();
        if (StringUtils.isNotEmpty(paramIds)) {

            String[] paramIdArray = paramIds.split(",");

            for (int i = 0; i < paramIdArray.length; i++) {

                ParamDO paramDO = paramMapper.getById(Long.parseLong(paramIdArray[i]));

                if (null == paramDO) {

                    context.addErrorMsg("params error,param not exist , id =" + paramIdArray[i]);
                    result = false;
                }
            }

            if (messageCreateValidator.paramsAllExistVerify(paramIds)) {

                if (!messageCreateValidator.paramsVersionVerify(paramIds)) {

                    context.addErrorMsg("params error ,paramVersion not the same");
                    result = false;

                }
            }

        }
        return result;
    }

    public boolean validateMsgIdUpdate(MessageDTO messageDTO) {

        boolean result = false;

        if (null != messageDTO.getId()) {

            MessageDO message = messageMapper.getById(messageDTO.getId());

            if (message.getMsgId().equals(messageDTO.getMsgId())) {

                result = true;
            }
        }
        return result;
    }


}